package com.example.healthapp.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.healthapp.R;
import com.example.healthapp.utils.InterfaceTool;
import com.example.healthapp.utils.SPManage;

// ✅ 正确导入 MainActivity
import com.example.healthapp.ui.activity.MainActivity;

public class LoginActivity extends AppCompatActivity {

    EditText login_account, login_password;
    TextView login_btn, login_register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        InterfaceTool.setFullScreen(this, false);
        permission();
        SPManage.getSP(LoginActivity.this, "account");
        initview();
        refreshView();
        event();
    }

    private void permission() {
        InterfaceTool.requestPermissions(this, new String[]{
                Manifest.permission.READ_SMS,
                Manifest.permission.READ_CONTACTS,
                Manifest.permission.VIBRATE,
                Manifest.permission.FOREGROUND_SERVICE
        });
    }

    private void initview() {
        login_account = findViewById(R.id.login_account);
        login_password = findViewById(R.id.login_password);
        login_btn = findViewById(R.id.login_btn);
        login_register = findViewById(R.id.login_register);
    }

    private void refreshView() {
        String account_local = SPManage.getSP(this, "account").getString("account");
        String password_local = SPManage.getSP(this, "account").getString("password");

        login_account.setText(account_local);
        login_password.setText(password_local);
    }

    private void event() {
        login_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                InterfaceTool.setIntent(LoginActivity.this, RegisterActivity.class);
            }
        });

        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String account = login_account.getText().toString();
                String password = login_password.getText().toString();

                String account_local = SPManage.getSP(LoginActivity.this, "account").getString("account");
                String password_local = SPManage.getSP(LoginActivity.this, "account").getString("password");

                if (account.equals("") || password.equals("") || !account.equals(account_local) || !password.equals(password_local)) {
                    Toast.makeText(LoginActivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();

                    // 登录成功后，跳转到 MainActivity，并直接加载 OxygenDataViewerFragment
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);  // 跳转到 MainActivity
                    finish();  // 结束当前的 LoginActivity
                }
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            for (int i = 0; i < permissions.length; i++) {
                if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(LoginActivity.this, "获取权限成功", Toast.LENGTH_SHORT).show();
                } else
                    Toast.makeText(LoginActivity.this, "权限获取失败", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
